/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastroEmpregado.java
 *
 * Created on Oct 19, 2011, 6:48:13 PM
 */

package manutençãoempregados.GUI;

import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import manutençãoempregados.UTILS.CadastrarEmpregado;
import classesValidacao.ValidaDadosCadastro;
import classesDados.*;
import interfaces.IDadosCadastrais;

/**
 *
 * @author Leandro
 */
public class CadastroEmpregadoGUI extends javax.swing.JDialog {

    /** Creates new form CadastroEmpregado */
    public CadastroEmpregadoGUI(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setFalseEnabled();   
        cpfTxtField.setText("");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sexoButtonGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        cadastroButton = new javax.swing.JButton();
        cpfTxtField = new javax.swing.JTextField();
        nomeTextField = new javax.swing.JTextField();
        dtnascTextField = new javax.swing.JTextField();
        salarioTextField = new javax.swing.JTextField();
        dtadmTextField = new javax.swing.JTextField();
        dtdeslTextField = new javax.swing.JTextField();
        femininoButton = new javax.swing.JRadioButton();
        MasculinoButton = new javax.swing.JRadioButton();
        jLabel8 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setForeground(new java.awt.Color(0, 153, 204));
        jLabel1.setText("Cadastro");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setForeground(new java.awt.Color(0, 51, 51));
        jLabel2.setText("CPF:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setForeground(new java.awt.Color(0, 51, 51));
        jLabel3.setText("Nome:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(0, 51, 51));
        jLabel4.setText("Data de Nascimento:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setForeground(new java.awt.Color(0, 51, 51));
        jLabel5.setText("Data de Admissão:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel6.setForeground(new java.awt.Color(0, 51, 51));
        jLabel6.setText("Salário Atual:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel7.setForeground(new java.awt.Color(0, 51, 51));
        jLabel7.setText("Data de Desligamento:");

        cadastroButton.setText("Cadastrar");
        cadastroButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cadastroButtonActionPerformed(evt);
            }
        });

        cpfTxtField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cpfTxtFieldActionPerformed(evt);
            }
        });

        nomeTextField.setText("digite o nome");
        nomeTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nomeTextFieldActionPerformed(evt);
            }
        });
        nomeTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                nomeTextFieldFocusGained(evt);
            }
        });

        dtnascTextField.setText("digite a data de nasc");
        dtnascTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dtnascTextFieldActionPerformed(evt);
            }
        });
        dtnascTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                dtnascTextFieldFocusGained(evt);
            }
        });

        salarioTextField.setText("digite o salário");
        salarioTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salarioTextFieldActionPerformed(evt);
            }
        });
        salarioTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                salarioTextFieldFocusGained(evt);
            }
        });

        dtadmTextField.setText("digite a data de adm");
        dtadmTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dtadmTextFieldActionPerformed(evt);
            }
        });
        dtadmTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                dtadmTextFieldFocusGained(evt);
            }
        });

        dtdeslTextField.setText("digite a data de desl");
        dtdeslTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dtdeslTextFieldActionPerformed(evt);
            }
        });
        dtdeslTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                dtdeslTextFieldFocusGained(evt);
            }
        });

        sexoButtonGroup.add(femininoButton);
        femininoButton.setText("Feminino");
        femininoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                femininoButtonActionPerformed(evt);
            }
        });

        sexoButtonGroup.add(MasculinoButton);
        MasculinoButton.setText("Masculino");
        MasculinoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MasculinoButtonActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(0, 51, 51));
        jLabel8.setText("Sexo");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(229, Short.MAX_VALUE)
                .addComponent(cadastroButton, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(74, 74, 74))
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4))
                        .addGap(17, 17, 17)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(dtadmTextField, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dtnascTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                            .addComponent(salarioTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                            .addComponent(dtdeslTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3)
                                .addComponent(jLabel2)
                                .addComponent(jLabel8))
                            .addGap(115, 115, 115)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(nomeTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                                .addComponent(cpfTxtField, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(femininoButton)
                                    .addGap(18, 18, 18)
                                    .addComponent(MasculinoButton)))
                            .addGap(41, 41, 41)))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cpfTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(nomeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(femininoButton)
                    .addComponent(MasculinoButton)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(dtnascTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(dtadmTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(salarioTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(dtdeslTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(49, 49, 49)
                .addComponent(cadastroButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void cadastroButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cadastroButtonActionPerformed
        /*após entrar com todos os dados,eles serão validados e criado o objeto Empr aqui, onde será
            enviado para o CadastrarEmpregado.java e lá ele será colocado no xml*/
            
        if(femininoButton.isSelected()){
            sexo = "Feminino";
        }
        
        if(MasculinoButton.isSelected()){
            sexo = "Masculino";
        }
    
        Date dataDeNascimento = new Date(dataNasc);
        Date dataDeAdmissão = new Date(dataAdm);
        Date dataDeDesligamento = new Date(dataDesl);
        
        
        IDadosCadastrais empr = new Empregado(cpf, nome, sexo, dataDeNascimento, dataDeAdmissão, salario);
        System.out.println("flkjf");
        cadEmpr.CadastrarObjeto(empr);
                
        closeWindowOption();
}//GEN-LAST:event_cadastroButtonActionPerformed

private void cpfTxtFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cpfTxtFieldActionPerformed
      
        try{
          if(cadEmpr.verificaExistencia(cpfTxtField.getText()))
              throw new Exception("CPF já existente");
            if(CPF.validaCPF(cpfTxtField.getText())){
                 cpf = new CPF(cpfTxtField.getText());
                 nomeTextField.setEnabled(true);
                 nomeTextField.requestFocusInWindow();
                  }
            else
                throw new Exception("CPF não é válido");
                
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage(), "CPF", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_cpfTxtFieldActionPerformed

private void nomeTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nomeTextFieldActionPerformed
      try{           
            if(ValidaDadosCadastro.validaNome(nomeTextField.getText()) && nomeTextField.getText().isEmpty() == false){
                 nome = nomeTextField.getText();
                 femininoButton.setEnabled(true);
                 MasculinoButton.setEnabled(true);
                  }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage(), "Nome", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_nomeTextFieldActionPerformed

private void nomeTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nomeTextFieldFocusGained
        generalFocusGain(nomeTextField);
}//GEN-LAST:event_nomeTextFieldFocusGained

private void dtnascTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dtnascTextFieldActionPerformed
        try{
          if(ValidaDadosCadastro.validaDatanasc(dtnascTextField.getText())){
              dataNasc = dtnascTextField.getText();
              dtadmTextField.setEnabled(true);
              dtadmTextField.requestFocusInWindow();
          }
          } catch (Exception ex) {
                   JOptionPane.showMessageDialog(this,ex.getMessage(), "Data de Nascimento", JOptionPane.ERROR_MESSAGE);
          }
}//GEN-LAST:event_dtnascTextFieldActionPerformed

private void dtnascTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_dtnascTextFieldFocusGained
        generalFocusGain(dtnascTextField);
}//GEN-LAST:event_dtnascTextFieldFocusGained

private void dtadmTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dtadmTextFieldActionPerformed
         try{
          if(ValidaDadosCadastro.validaDataadm(dtadmTextField.getText(),dataNasc)){
              dataAdm = dtadmTextField.getText();
              salarioTextField.setEnabled(true);
              salarioTextField.requestFocusInWindow();
          }
          } catch (Exception ex) {
                   JOptionPane.showMessageDialog(this,ex.getMessage(), "Data de Admissão", JOptionPane.ERROR_MESSAGE);
          }
}//GEN-LAST:event_dtadmTextFieldActionPerformed

private void dtadmTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_dtadmTextFieldFocusGained
        generalFocusGain(dtadmTextField);
}//GEN-LAST:event_dtadmTextFieldFocusGained

private void salarioTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salarioTextFieldActionPerformed
         try{
             if(Salario.validaSalario(Double.parseDouble(salarioTextField.getText()))){
               salario = new Salario(Double.parseDouble(salarioTextField.getText()));
               dtdeslTextField.setEnabled(true);
               dtdeslTextField.requestFocusInWindow();
             }
          } catch (Exception ex) {
                   JOptionPane.showMessageDialog(this,ex.getMessage(), "Salário", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_salarioTextFieldActionPerformed

private void salarioTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_salarioTextFieldFocusGained
        generalFocusGain(salarioTextField);
}//GEN-LAST:event_salarioTextFieldFocusGained

private void dtdeslTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dtdeslTextFieldActionPerformed
       try{
             if(ValidaDadosCadastro.validaDataadm(dtdeslTextField.getText(),dataAdm)){
               dataDesl = dtdeslTextField.getText();
               cadastroButton.setEnabled(true);
               cadastroButton.requestFocusInWindow();
               dtnascTextField.setEnabled(true);
             }
          } catch (Exception ex) {
                   JOptionPane.showMessageDialog(this,ex.getMessage(), "Data de desligamento", JOptionPane.ERROR_MESSAGE);
    }  
}//GEN-LAST:event_dtdeslTextFieldActionPerformed

private void dtdeslTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_dtdeslTextFieldFocusGained
        generalFocusGain(dtdeslTextField);
}//GEN-LAST:event_dtdeslTextFieldFocusGained

private void femininoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_femininoButtonActionPerformed
        dtnascTextField.setEnabled(true);
        dtnascTextField.requestFocusInWindow();
}//GEN-LAST:event_femininoButtonActionPerformed

private void MasculinoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MasculinoButtonActionPerformed
        dtnascTextField.setEnabled(true);
        dtnascTextField.requestFocusInWindow();
}//GEN-LAST:event_MasculinoButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                CadastroEmpregadoGUI dialog = new CadastroEmpregadoGUI(new javax.swing.JFrame(), true);
                                
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.out.println("yes");
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
       
    public void generalFocusGain(JTextField textField){
        textField.setText("");
    }
    
     private void closeWindowOption() {
        int response = JOptionPane.showConfirmDialog(this,"Deseja cadastrar um novo empregado?","Usuário cadastrado com sucesso", JOptionPane.YES_NO_OPTION);
        
        if(response == JOptionPane.YES_OPTION){
            System.out.println("yes");
            setFalseEnabled();
            cpfTxtField.requestFocus();
        }
        
        if(response == JOptionPane.NO_OPTION){
            System.out.println("no");
            setFalseEnabled();
            this.setVisible(false);            
        }
    }
    
    private void setFalseEnabled() {
        nomeTextField.setEnabled(false);
        femininoButton.setEnabled(false);
        MasculinoButton.setEnabled(false);
        dtnascTextField.setEnabled(false);        
        dtadmTextField.setEnabled(false);
        dtdeslTextField.setEnabled(false);        
        salarioTextField.setEnabled(false);
        cadastroButton.setEnabled(false);
    }
    
    private CPF cpf = new CPF();
    private String nome;
    private String sexo;
    private String dataNasc;
    private String dataAdm;
    private Salario salario = new Salario();
    private String dataDesl;
    private CadastrarEmpregado cadEmpr = new CadastrarEmpregado();

        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton MasculinoButton;
    private javax.swing.JButton cadastroButton;
    private javax.swing.JTextField cpfTxtField;
    private javax.swing.JTextField dtadmTextField;
    private javax.swing.JTextField dtdeslTextField;
    private javax.swing.JTextField dtnascTextField;
    private javax.swing.JRadioButton femininoButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField nomeTextField;
    private javax.swing.JTextField salarioTextField;
    private javax.swing.ButtonGroup sexoButtonGroup;
    // End of variables declaration//GEN-END:variables

}
